#include "serverwidget.h"
#include "ui_serverwidget.h"


ServerWidget::ServerWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ServerWidget)
{
    ui->setupUi(this);

    //用来避免未连接时野指针造成程序崩溃，在函数中也做判断
    tcpServer = nullptr;
    tcpSocket = nullptr;

    //监听套接字，指定父对象让其自动回收空间
    tcpServer =new QTcpServer(this);

    tcpServer->listen(QHostAddress::Any,8888);

    setWindowTitle("服务器：端口8888");

    connect(tcpServer,&QTcpServer::newConnection,
            [=]()
            {
                //取出建立好连接的套接字
                tcpSocket = tcpServer->nextPendingConnection();

                //获取对方的IP和端口
                QString ip = tcpSocket->peerAddress().toString();
                quint16 port = tcpSocket->peerPort();

                QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
                ui->textEditRead->setText(temp);

                //注意此处的位置，取出套接字后才可以读
                connect(tcpSocket,&QTcpSocket::readyRead,
                        [=]()
                        {
                           //从通信套接字中取出内容
                            QByteArray array = tcpSocket->readAll();
                            ui->textEditRead->append(array);
                        }
                        );
            }
            );

}

ServerWidget::~ServerWidget()
{
    delete ui;
}

void ServerWidget::on_buttonSend_clicked()
{
    if(tcpSocket == nullptr)
    {
        return;
    }

    //获取编辑区内容
    QString str = ui->textEditWrite->toPlainText();
    //给对方发送数据，使用套接字tcpSocket
    tcpSocket->write(str.toUtf8().data());

}

void ServerWidget::on_pushButton_2_clicked()
{
    if(tcpSocket == nullptr)
    {
        return;
    }

    //主动和客户端断开连接,可再次连接
    tcpSocket->disconnectFromHost();
    tcpSocket->close();
    tcpSocket = nullptr;

}
